<?php

/*
 * This class is used for common same function.
 */

class Model_Login extends Model_Base {

    protected $_name = 'tbl_login';
    protected $_primaryKey = 'lgn_id';

    /**
     * Function to check email exist for forgot password.
     *
     * @param  int|string $email
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function checkEmailForForgotPassword($email) {

        $select = $this->select()->setIntegrityCheck(false)->from(array('lgn' => 'tbl_login'))
                ->where('lgn_email = ?', $email)
                ->where('lgn_active = 1');
        $result = $this->fetchRow($select);
        if (!empty($result)) {
            return $result->toArray();
        } else {
            return array();
        }
    }

    /**
     * Function to check user exist with email
     *
     * @param  string $email
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function checkEmailExist($email) {
        $select = $this->select()->setIntegrityCheck(false)->from(array('usr' => 'tbl_login'))
                ->where('lgn_email = ?', $email);
        $result = $this->fetchRow($select);
        if (!empty($result)) {
            return $result->lgn_id;
        } else {
            return false;
        }
    }

    /**
     * Function to check user exist with email other than current email used in edit mode
     *
     * @param  string $email
     * @param  string $email
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function checkEmail($currentEmail = '', $email) {

        $select = $this->select()->setIntegrityCheck(false)->from(array('lgn' => 'tbl_login'))
                ->where('lgn_email = ? ', $email)
                ->orWhere('lgn_email = ? ', $currentEmail);
        $result = $this->fetchAll($select);
        if (count($result) > 1) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Function to check user exist with usr_id
     *
     * @param  int|string $email
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function checkUserIdExist($userId) {

        $select = $this->select()->setIntegrityCheck(false)->from(array('usr' => 'tbl_user'))
                ->where('usr_id = ?', $userId);
        $result = $this->fetchRow($select);
        if (!empty($result)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('lgn' => 'tbl_login'))
                ->where('lgn_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    //Delete loginid with customer and constractor data
    public function deleteloginid($prdtArray) {
        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_admin) {
                $adminData = Array();
                $adminData = $this->fetchEntryById($_admin);
                if (count($adminData) > 0) {
                    $data = array('lgn_active' => '2');
                    $this->update($data, 'lgn_id =' . (int) $_admin);
                }
            }
        } else {
            $adm_id = $prdtArray;
            $adminData = $this->fetchEntryById($adm_id);
            if (count($adminData) > 0) {
                $data = array('lgn_active' => '2');
                $this->update($data, 'lgn_id =' . $adm_id);
            }
        }
    }

    public function getUserData($data = Array()) {

        if (count($data) == 0) {
            return array();
        }

        $finalArray = Array();
        $loginData = $this->fetchEntryById($data['lgn_id']);
        if ($loginData) {

            if ($loginData['lgn_type'] == '1') {
                $adminObj = new Model_Admin();
                $dataArray = Array();
                $dataArray['lgn_id'] = $data['lgn_id'];
                $finalArray = $adminObj->getAdminAuthData($dataArray);
            } else if ($loginData['lgn_type'] == '2') {
                //User is of the type Contractor
                $contractorObj = new Model_Contractor();
                $dataArray = Array();
                $dataArray['lgn_id'] = $data['lgn_id'];
                $finalArray = $contractorObj->getContractorAuthData($dataArray);
            } else if ($loginData['lgn_type'] == '3') {
                //User is of the type Customer
                $customerObj = new Model_Customer();
                $dataArray = Array();
                $dataArray['lgn_id'] = $data['lgn_id'];
                $finalArray = $customerObj->getCustomerAuthData($dataArray);
            }
        }
        return $finalArray;
    }
    
    
    //get customer name and contractor name
    
     public function fetchCustomerContratcor($where = "") {
        //echo $where;exit;
        $select = $this->select()
                ->from(array('lgn' => 'tbl_login'))
                ->setIntegrityCheck(false)
                ->joinLeft(array('cust' => 'tbl_customer'), "lgn.lgn_id = cust.lgn_id",array('cust_name' => "concat(cust_firstname,' ',cust_lastname)","cust_phone"))
                ->joinLEFT(array('cont' => 'tbl_contractor'), "lgn.lgn_id = cont.lgn_id",array('cont_name' => "concat(cont_firstname,' ',cont_lastname)","cont_phone"))
                ->where('lgn_active != 2')
                ->where('lgn_type != 1');
        if ($where != '') {
            $select->where($where);
        }

        //echo $select;exit;
        $data = parent::fetchAll($select);
        $finalArray = array();
        if (!empty($data)) {
            foreach($data->toArray() as $key =>$value){
                if($value['cust_name'] != ''){
                    $finalArray[$value['lgn_id']]['name'] = $value['cust_name'] . "(Customer)";
                    $finalArray[$value['lgn_id']]['contact_no'] = $value['cust_phone'] ;
                }else{
                    $finalArray[$value['lgn_id']]['name'] = $value['cont_name']. "(Contractor)";
                    $finalArray[$value['lgn_id']]['contact_no'] = $value['cont_phone'];
                }
                
            }
          
            return $finalArray;
        } else {
            return array();
        }
    }

    
    
    

}

